package dse.messages.multiversion;

import dse.DataObject;
import dse.Experiment;
import dse.ExperimentRecorder;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessageSender;
import dse.messages.MessageType;

public class TurnVersionRepOnMessageHandler extends MessageSender implements
		MessageHandler {
	private DataObject dobj;
	private int version;
	private boolean onoff;
	private VersionReader versionReader;
	
	public TurnVersionRepOnMessageHandler (
			Experiment experiment,
			DataObject dobj,
			int version,
			boolean onoff,
			VersionReader versionReader
			)
	{
		super(experiment);
		
		this.dobj = dobj;
		this.version = version;
		this.onoff = onoff;
		this.versionReader = versionReader;
	}
	
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		//recorder.debug(this, "msgIntrpt: " + message, time);
		versionReader.readVersion(recorder, -1, 0, time);
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message, double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgRcvd: " + message, time);
		if (message.messageType() != MessageType.TurnVersionRepOn) {
			recorder.warn(
					"TurnVersionRepOnMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}			
		
		message.to().writeVersion(dobj.id(), this.version, onoff);
		versionReader.readVersion(recorder, -1, 0, time);
	}
}